let app = getApp();
Page({
    data: {
        nickname: '',
        enrollmentYear: '',
        university: '',
        college: '',
        major: '',
        email: '',
        phone: '',
        userInfo:{}
    },
    onLoad: function (options) {
        let that = this;
        wx.request({
            url: app.globalData.serverUrl + "user/" + app.globalData.userInfo.id,
            method: 'get',
            success: function (res) {
                if (res.data.status == 200)
                    console.log(res.data.data)
                app.globalData.userInfo = res.data.data;
                // that.setData({
                //     userInfo: res.data.data
                // });
                that.setData({
                    userInfo: app.globalData.userInfo,
                    nickname: app.globalData.userInfo.nickname,
                    enrollmentYear: app.globalData.userInfo.enrollmentYear,
                    university: app.globalData.userInfo.university,
                    college: app.globalData.userInfo.major,
                    email: app.globalData.userInfo.email,
                    phone: app.globalData.userInfo.phone,
                    major: app.globalData.userInfo.major
                });
            }
        });

    },
    update:function () {
        let that = this;
        that.setData({
            userInfo: {
                id: app.globalData.userInfo.id,
                nickname: that.data.nickname,
                enrollmentYear: that.data.enrollmentYear,
                university: that.data.university,
                college: that.data.college,
                email: that.data.email,
                phone: that.data.phone,
                major: that.data.major
            }
        });
        wx.request({
            url: app.globalData.serverUrl + 'user',
            method: 'put',
            data: that.data.userInfo,
            headers:{
                'content-type':'application/json'
            },
            success: function (res) {
                if (res.data.status == 200) {
                    wx.showToast({
                        title: res.data.data.success,
                        icon: 'none',
                        duration: 1500
                    });
                    wx.switchTab({
                        url: '../me/me'
                    })
                }
            }
        });
        console.log(this.data.userInfo);
    },
    bindDateChange: function(e) {
        this.setData({
            enrollmentYear: e.detail.value
        })
    },
});
